﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SGArchiveSystem7xDB;
using SGArchiveSystem7xDB.Linq;

namespace RFID信息初始化工作台
{
    public partial class frmLogin : Form
    {
        public bool _bTest = false;

        public string _sEns01 = "RFID Information initialization";
        public string _sEns02 = "User:";
        public string _sEns03 = "Password:";
        public string _sEns04 = "Login";
        public string _sEns05 = "DB Config";
        public string _sEns06 = "Test";
        public string _sEns07 = "No need to connect to the database when testing";


        public frmLogin()
        {
            InitializeComponent();

            if(Program._Language == Program.Language.Eng)
            {
                lbl_Title.Text = _sEns01;
                btn_Config.Text = _sEns05;
                btn_Login.Text = _sEns04;
                btn_Test.Text = _sEns06;
                lbl_Pwd.Text = _sEns03;
                lbl_User.Text = _sEns02;
                lbl_Info.Text = _sEns07;
            }
        }

        private void frmLogin_Load(object sender, EventArgs e)
        {
            txt_User.Focus();
            
        }

        private void txt_User_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                txt_Pwd.Focus();
        }

        private void txt_Pwd_KeyDown(object sender, KeyEventArgs e)
        {
            if(e.KeyCode == Keys.Enter)
            {
                Login();
            }
        }

        private void Login()
        {
            try
            {
                var en = T_UserDal.GetLoginUser(txt_User.Text, txt_Pwd.Text);
                if (en != null)
                {
                    Commons._enUser = en;
                    this.Close();
                }
                else
                {
                    if (Program._Language == Program.Language.Chs)
                        MessageBox.Show("用户名或密码错误!");
                    else
                        MessageBox.Show("Username or password is incorrect");
                }
                    
            }
            catch(Exception ex) {
                if (Program._Language == Program.Language.Chs)
                    MessageBox.Show("登录失败!");
                else
                    MessageBox.Show("Login Error");
            }
        }

        private void btn_Config_Click(object sender, EventArgs e)
        {
            frmConfig frmConfig = new frmConfig();
            frmConfig.ShowDialog();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Login();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            _bTest = true;
            this.Close();
        }
    }
}
